package com.ttfeed.server.common.domain.base;

import com.ttfeed.server.common.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 8822366994453895673L;

    @ApiModelProperty(value = "返回码")
    private int code;

    @ApiModelProperty(value = "返回提示信息")
    private String msg;

    @ApiModelProperty(value = "返回数据详情")
    private T data;

    public ApiResult() {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMessage();
    }

    public ApiResult(T data) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMessage();
        this.data = data;
    }

    public ApiResult(int code, String msg) {
        this(code, msg, null);
    }

    public ApiResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ApiResult(ResultEnum resultEnum) {
        this(resultEnum, null);
    }

    public ApiResult(ResultEnum resultEnum, T data) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMessage();
        this.data = data;
    }

    public static ApiResult<?> fail() {
        return fail(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMessage());
    }

    public static <T> ApiResult<T> fail(String msg) {
        return fail(ResultEnum.FAIL.getCode(), msg);
    }

    public static <T> ApiResult<T> fail(ResultEnum resultEnum) {
        return fail(resultEnum.getCode(), resultEnum.getMessage());
    }

    public static <T> ApiResult<T> fail(int code, String msg) {
        ApiResult<T> r = new ApiResult<>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static <T> ApiResult<T> success() {
        return new ApiResult<>();
    }

    public static <T> ApiResult<T> success(T t) {
        ApiResult<T> ApiResult = new ApiResult<>();
        ApiResult.setData(t);
        ApiResult.setCode(ResultEnum.SUCCESS.getCode());
        return ApiResult;
    }

    public int getCode() {
        return code;
    }

    public ApiResult<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }


    public ApiResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public ApiResult<T> setData(T data) {
        this.data = data;
        return this;
    }

}
